﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LX.Core.Utils;
using LX.Web.Mvc;
using S4Store.Models.Sellers;
using S4Store.Service;
using S4Store.Domain;

namespace S4Store.Controllers
{
    public class DeadStockController : ReportBaseController
    {
        //
        // GET: /DeadStock/
        public DeadStockService DeadStockService { get; set; }
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult InputMonthData(int y, int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(DeadStockService);
                var querysource = DeadStockService.GetQuery();
                querysource = querysource.Where(x => x.StoreId == Store.Id);
                var queryresult = querysource.Where(o => o.Month.Year == y && o.Month.Month == m);
                List<Domain.AfterSaleKpi.DeadStock> list = new List<Domain.AfterSaleKpi.DeadStock>();
                if (queryresult.Where(o => o.Cla == 0).Count() == 0)
                {
                    list.Add(new Domain.AfterSaleKpi.DeadStock() { Month = new DateTime(y, m, 1), Cla = 0 });
                    list.Add(new Domain.AfterSaleKpi.DeadStock() { Month = new DateTime(y, m, 1), Cla = 0 });
                    list.Add(new Domain.AfterSaleKpi.DeadStock() { Month = new DateTime(y, m, 1), Cla = 0 });
                }
                else
                {
                    foreach (var item in queryresult.Where(o => o.Cla == 0))
                    {
                        list.Add(item);
                    }
                }

                if (queryresult.Where(o => o.Cla == 1).Count() == 0)
                {
                    list.Add(new Domain.AfterSaleKpi.DeadStock() { Month = new DateTime(y, m, 1), Cla = 1 });
                    list.Add(new Domain.AfterSaleKpi.DeadStock() { Month = new DateTime(y, m, 1), Cla = 1 });
                    list.Add(new Domain.AfterSaleKpi.DeadStock() { Month = new DateTime(y, m, 1), Cla = 1 });
                }
                else
                {
                    foreach (var item in queryresult.Where(o => o.Cla == 1))
                    {
                        list.Add(item);
                    }
                }
                ViewBag.Date = new DateTime(y, m, 1);
                return View(list);
            }
        }

        public string MonthTd(int y, int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(DeadStockService);
                var querysource = DeadStockService.GetQuery();
                querysource = querysource.Where(x => x.StoreId == Store.Id);
                var queryresult = querysource.Where(o => o.Month.Year == y && o.Month.Month == m).FirstOrDefault();
                if (queryresult == null)
                {
                    return "style='background:" + S4Store.Models.Chart.Color.Red + ";padding:10px;'";
                }
                else if (queryresult.IsConfirmed)
                {
                    return "style='background:" + S4Store.Models.Chart.Color.Green + ";padding:10px'";
                }
                else
                {
                    return "style='background:" + S4Store.Models.Chart.Color.Yellow + ";padding:10px;'";
                }

            }
        }

        public JsonResult ModifyDeadStockMonthInput(List<Domain.AfterSaleKpi.DeadStock> afterSaleCarlist, int y, int m, bool isconfirm = false)
        {
            using (var t = new Transtion())
            {
                t.Inject(DeadStockService);
                DateTime now = new DateTime(y, m, 1);
                var list = new List<S4Store.Domain.AfterSaleKpi.DeadStock>();
                foreach (var model in afterSaleCarlist)
                {
                    model.Month = now;
                    model.IsConfirmed = isconfirm;
                    model.StoreId = Store.Id;
                    list.Add(model);
                }
                DeadStockService.SubmitDeadStockList(y, m, Store.Id, list);
                t.Commit();
            }

            return new JsonResult
            {
                Data = new
                {
                    success = true
                }
            };
        }

    }
}
